package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.NoticeData
import com.cheyun.netsalev3.bean.RowX
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import com.cheyunkeji.er.utils.TimeUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject
import java.util.*

class MessageListRepository : BasePageRepository<RowX>() {
    var uid = MySharedPreferences.getLoginData()?.member?.uid ?: 0
    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<RowX>) -> Unit) {
        ApiClient.getInstance().noticeDatalist(uid, 1)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<NoticeData>>() {
                    override fun onSuccess(t: BaseResp<NoticeData>) {
                        t.data?.rows?.apply {
                            total.postValue(t.data!!.total)
                            for (item in this) {
                                item.dateline = FunctionUtils.getMessageTime(item.dateline)
                            }
                            callback(this.toMutableList())
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }
                })
    }

    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, RowX>) {
        ApiClient.getInstance().noticeDatalist(uid, params.key)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<NoticeData>>() {
                    override fun onSuccess(t: BaseResp<NoticeData>) {
                        t.data?.rows?.apply {
                            for (item in this) {
                                item.dateline = FunctionUtils.getMessageTime(item.dateline)
                            }
                            callback.onResult(this.toMutableList(), params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, RowX>) {
        ApiClient.getInstance().noticeDatalist(uid, params.key)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<NoticeData>>() {
                    override fun onSuccess(t: BaseResp<NoticeData>) {
                        t.data?.rows?.apply {
                            for (item in this) {
                                item.dateline = FunctionUtils.getMessageTime(item.dateline)
                            }
                            callback.onResult(this.toMutableList(), params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    fun getNotice(id: Int, moth: () -> Unit) {
        ApiClient.getInstance().noticeNum(uid, id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    fun allRead(moth: () -> Unit){
        ApiClient.getInstance().noticeAllRead().subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }
    fun allClear(moth: () -> Unit){
        ApiClient.getInstance().noticeAllClear().subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<JSONObject>>() {
                    override fun onSuccess(t: BaseResp<JSONObject>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }
}